package com.fire.social.controller;

import com.fire.social.config.Result;
import com.fire.social.config.Results;
import com.fire.social.entity.node.Hobby;
import com.fire.social.entity.node.User;
import com.fire.social.service.HobbyService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class HobbyController {

    @Autowired
    private HobbyService hobbyService;

    @GetMapping("/hobby/getall")
    public String getAll(HttpServletRequest request, Map<String, Object> map) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) return "login";
        hobbyService.findHobbyByAccount(user, map);
        return "hobbys";
    }

    @PostMapping("/hobby/addhobby")
    @ResponseBody
    public Result addHobby(@RequestParam("hname") String hname,
                           @RequestParam("htype") String htype) {
        Hobby hobby = hobbyService.createHobby(hname, htype);
        if (hobby == null) {
            return Results.failure();
        }
        return Results.success(hobby);
    }


    @GetMapping("/hobby/search")
    @ResponseBody
    public Result search(@RequestParam("hname") String hname) {
        List<Hobby> hobbies = hobbyService.findHobbyByName(".*" + hname + ".*");
        return Results.success(hobbies);
    }

}
